package com.ruoyi.vote.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.enums.UserType;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.vote.common.constants.VoteConstants;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.vote.admin.domain.bo.VoteBannerBo;
import com.ruoyi.vote.admin.domain.vo.VoteBannerVo;
import com.ruoyi.vote.admin.domain.VoteBanner;
import com.ruoyi.vote.admin.mapper.VoteBannerMapper;
import com.ruoyi.vote.admin.service.IVoteBannerService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * banner轮播图Service业务层处理
 *
 * @author 筑巢
 * @date 2023-07-18
 */
@RequiredArgsConstructor
@Service
public class VoteBannerServiceImpl implements IVoteBannerService {

    private final VoteBannerMapper baseMapper;

    /**
     * 查询banner轮播图
     */
    @Override
    public VoteBannerVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询banner轮播图列表
     */
    @Override
    public TableDataInfo<VoteBannerVo> queryPageList(VoteBannerBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<VoteBanner> lqw = buildQueryWrapper(bo);
        Page<VoteBannerVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询banner轮播图列表
     */
    @Override
    public List<VoteBannerVo> queryList(VoteBannerBo bo) {
        LambdaQueryWrapper<VoteBanner> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<VoteBanner> buildQueryWrapper(VoteBannerBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<VoteBanner> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), VoteBanner::getUrl, bo.getUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getPath()), VoteBanner::getPath, bo.getPath());
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), VoteBanner::getTitle, bo.getTitle());
        lqw.eq(bo.getStatus() != null, VoteBanner::getStatus, bo.getStatus());
        if(UserType.XCX_MEMBER.equals(LoginHelper.getUserType())){
            lqw.eq(VoteBanner::getStatus, VoteConstants.VOTE_STATUS_SUCCESS);
        }
        return lqw;
    }

    /**
     * 新增banner轮播图
     */
    @Override
    public Boolean insertByBo(VoteBannerBo bo) {
        VoteBanner add = BeanUtil.toBean(bo, VoteBanner.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改banner轮播图
     */
    @Override
    public Boolean updateByBo(VoteBannerBo bo) {
        VoteBanner update = BeanUtil.toBean(bo, VoteBanner.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(VoteBanner entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除banner轮播图
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
